<?php
namespace Adm\Controller;
use Think\Controller;

class PublicController extends Controller {
	
	/**
	 * 用户登录页面
	 * http://127.0.0.1/test/index.php/public/login
	 */
	public function login() {
		$this->display();
	}
	
	/**
	 * 登录提交的地址
	 */
	public function login_submit() {
		if(!IS_POST) $this->error('页面不存在，请稍后再试', U('Public/login'));
		
		$username = I('username','');
		$password = I('password','');
		
		$user_info = M('user')->where("username = '$username' AND password = '".md5($password)."'")->find();
		
		if(!$user_info) $this->error('用户名或密码错误', U('Public/login'));
		
		if(!$user_info['status']) $this->error('该用户已被禁用，暂时不可登录', U('Public/login'));
		
		// 更新登录信息
		M('user')->save(array("uid"=> $user_info["uid"], "login_time"=> time(), "login_ip" => ip2long(get_client_ip())));
		
		// 写入session
		session('uid', $user_info['uid']);   // 用户id（uid)
		session('username', $user_info['username']);   // 用户名
		
		$this->success('登录成功', U('Index/index'));
	}
}